//
//  ForecastController.m
//  NewBiuWeather
//
//  Created by 楼楚 on 16/4/9.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ForecastController.h"
#import "WeatherNumberMeaningTransform.h"

@interface ForecastController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView     *tableView;
@property (nonatomic, strong) UITableViewCell *tableViewCell;
@property (nonatomic, strong) UILabel         *weekDayInfoLabel;
@property (nonatomic, strong) UILabel         *dateLabel;
@property (nonatomic, strong) UILabel         *dayminTempLabel;
@property (nonatomic, strong) UILabel         *daymaxTempLabel;
@property (nonatomic, strong) UILabel         *dayweatherLabel;
@property (nonatomic, strong) UILabel         *dayweatherInfoLabel;

@property (nonatomic, strong) UILabel         *hourTempLabel;
@property (nonatomic, strong) UILabel         *hourTimeLabel;
@property (nonatomic, strong) UILabel         *windDiscrLabel;
@property (nonatomic, strong) UILabel         *humidityLabel;//%

@end

@implementation ForecastController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initTableView];
    // Do any additional setup after loading the view.
}

- (void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator   = NO;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.hourlyFore.hourlyFor.count;
     }
    return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    //    如果如果没有多余单元，则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }
    
    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    NSUInteger row = [indexPath row];

    if (indexPath.section == 1) {
        NSDictionary *todayInfo =self.oneWeekFore.oneWeek[row];
        NSDictionary *dic1 = [todayInfo objectForKey:@"tmp"];
        NSDictionary *dic2 = [todayInfo objectForKey:@"cond"];
        NSString *string = [todayInfo objectForKey:@"date"];
        NSString *newStr = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSDateFormatter *inputFormatter =  [[NSDateFormatter alloc]init];
        [inputFormatter setDateFormat:@"yyyyMMdd"];
        NSDate *inputDate = [inputFormatter dateFromString:newStr];
        NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
        NSInteger frominterval = [fromzone secondsFromGMTForDate: inputDate];
        NSDate *fromDate = [inputDate  dateByAddingTimeInterval: frominterval];
        NSDateFormatter *new = [[NSDateFormatter alloc]init];
        [new setDateFormat:@"EEE"];
        NSString *weekStr = [new stringFromDate:fromDate];
        
        // 星期几
        if (iPhone4_4s || iPhone5_5s) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont systemFontOfSize:28.f];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else if (iPhone6_6s) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont systemFontOfSize:34.f];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else if (iPhone6_6sPlus) {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont systemFontOfSize:40.f];
            self.weekDayInfoLabel.text          = weekStr;
            
        } else {
            
            self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
            self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
            self.weekDayInfoLabel.font          = [UIFont systemFontOfSize:28.f];
            self.weekDayInfoLabel.text          = weekStr;
        }
        
        [self.weekDayInfoLabel sizeToFit];
        [cell.contentView addSubview:self.weekDayInfoLabel];
        
        // 日期
        self.dateLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 12.f)];
        self.dateLabel.textAlignment = NSTextAlignmentCenter;
        self.dateLabel.font          = [UIFont systemFontOfSize:8.f];
        self.dateLabel.text          = @"2015.2.25";
        self.dateLabel.textColor     = [UIColor whiteColor];
        
        UIView *dateBlackView         = [[UIView alloc] initWithFrame:CGRectMake(Width - 50, 0, 50, 12.f)];
        dateBlackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.95];
        [dateBlackView addSubview:self.dateLabel];
        
        // 最低温度
        if (iPhone4_4s || iPhone5_5s) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont systemFontOfSize:28];
            //        self.minTempLabel.text          = @"-200.0°";
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
        } else if (iPhone6_6s) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(110, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont systemFontOfSize:34];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
            
        } else if (iPhone6_6sPlus) {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont systemFontOfSize:40];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
        } else {
            
            self.dayminTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.dayminTempLabel.textAlignment = NSTextAlignmentLeft;
            self.dayminTempLabel.font          = [UIFont systemFontOfSize:28];
            self.dayminTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];    }
        
        [self.dayminTempLabel sizeToFit];
        [cell.contentView addSubview:self.dayminTempLabel];
        
        
        // 最高温度
        if (iPhone4_4s || iPhone5_5s) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont systemFontOfSize:28];
            //        self.maxTempLabel.text          = @"-200.0°";
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
        } else if (iPhone6_6s) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 20, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont systemFontOfSize:34];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
        } else if (iPhone6_6sPlus) {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 40, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont systemFontOfSize:40];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
            
        } else {
            
            self.daymaxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.daymaxTempLabel.textAlignment = NSTextAlignmentLeft;
            self.daymaxTempLabel.font          = [UIFont systemFontOfSize:28];
            self.daymaxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];    }
        
        [self.daymaxTempLabel sizeToFit];
        [cell.contentView addSubview:self.daymaxTempLabel];
        
        
        // 天气图标
        if (iPhone4_4s || iPhone5_5s) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        } else if (iPhone6_6s) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 40, 20, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:45.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
            
        } else if (iPhone6_6sPlus) {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 70, 24, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:50.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
            
        } else {
            
            self.dayweatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.dayweatherLabel.textAlignment = NSTextAlignmentCenter;
            self.dayweatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
            self.dayweatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        }
        [cell.contentView addSubview:self.dayweatherLabel];
        
        
        // 天气描述信息
        self.dayweatherInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 300, 12)];
        self.dayweatherInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.dayweatherInfoLabel.font          = [UIFont systemFontOfSize:10];
        self.dayweatherInfoLabel.text = [dic2 objectForKey:@"txt_d"];
        [cell.contentView addSubview:self.dayweatherInfoLabel];
        // 线条
        UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, Width / 4.f - 0.5, Width, 0.5)];
        line.backgroundColor = [UIColor blackColor];
        line.alpha           = 0.1;
        [cell.contentView addSubview:line];
    }else{
        
        self.hourTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.hourTimeLabel.textAlignment = NSTextAlignmentLeft;
       
        
        if (iPhone4_4s || iPhone5_5s) {
           self.hourTimeLabel.font          = [UIFont systemFontOfSize:28.f];
            
        } else if (iPhone6_6s){
            
            self.hourTimeLabel.font          = [UIFont systemFontOfSize:34.f];
        } else if (iPhone6_6sPlus){
            self.hourTimeLabel.font          = [UIFont systemFontOfSize:40.f];
            
        } else {
            self.hourTimeLabel.font          = [UIFont systemFontOfSize:28.f];
        }
        self.hourTimeLabel.text = @"";//时间
        [self.hourTimeLabel sizeToFit];
        [cell.contentView addSubview:self.hourTimeLabel];
        
       
        if (iPhone4_4s || iPhone5_5s) {
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont systemFontOfSize:28];

        } else if (iPhone6_6s){
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(110, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont systemFontOfSize:34];

            
        } else if (iPhone6_6sPlus){
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont systemFontOfSize:40];

            
        } else {
            self.hourTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
            self.hourTempLabel.textAlignment = NSTextAlignmentLeft;
            self.hourTempLabel.font          = [UIFont systemFontOfSize:28];

        }
        self.hourTempLabel.text = @"";//每3小时温度
        [self.hourTempLabel sizeToFit];
        
        [cell.contentView addSubview:self.hourTempLabel];
        
        
        if (iPhone4_4s || iPhone5_5s) {
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont systemFontOfSize:28];
            
        } else if (iPhone6_6s){
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80+ 20, 25, 200, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont systemFontOfSize:34];
            
        } else if (iPhone6_6sPlus){
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80+ 40, 25, 200, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont systemFontOfSize:40];
            
        } else {
            self.windDiscrLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
            self.windDiscrLabel.textAlignment = NSTextAlignmentLeft;
            self.windDiscrLabel.font          = [UIFont systemFontOfSize:28];
            
        }
        self.windDiscrLabel.text = @"";//天气描述
        [self.windDiscrLabel sizeToFit];
        [cell.contentView addSubview:self.windDiscrLabel];
        
        

        if (iPhone4_4s || iPhone5_5s) {
            self.humidityLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.humidityLabel.textAlignment = NSTextAlignmentCenter;
            self.humidityLabel.font          = [UIFont systemFontOfSize:18];
            
        } else if (iPhone6_6s){
            self.humidityLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250+ 40, 15, 60, 60)];
            self.humidityLabel.textAlignment = NSTextAlignmentCenter;
            self.humidityLabel.font          = [UIFont systemFontOfSize:28];
            
        } else if (iPhone6_6sPlus){
            self.humidityLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250+ 70, 15, 60, 60)];
            self.humidityLabel.textAlignment = NSTextAlignmentCenter;
            self.humidityLabel.font          = [UIFont systemFontOfSize:28];
            
        } else {
            self.humidityLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
            self.humidityLabel.textAlignment = NSTextAlignmentCenter;
            self.humidityLabel.font          = [UIFont systemFontOfSize:18];
        }
        
        self.humidityLabel.text = @"";//湿度
        [self.humidityLabel sizeToFit];
        [cell.contentView addSubview:self.humidityLabel];
        
        
        UIView *line1        = [[UIView alloc] initWithFrame:CGRectMake(0, Width / 4.f - 0.5, Width, 0.5)];
        line1.backgroundColor = [UIColor blackColor];
        line1.alpha           = 0.05;
        [cell.contentView addSubview:line1];
    }
        
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return Width/4.f;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y <= -60) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }
}

//状态栏
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
